package beautiful.butterfly.drds.data_exchange.util;


import beautiful.butterfly.drds.data_exchange.read_and_write_data.reader.Key;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class PrivilegeCheck
{
    public static boolean checkInsertPrivilege(String jdbcUrl, String userName, String password, List<String> tableNameList)
    {
        Connection connection = Jdbc.getConnection(jdbcUrl, userName, password);
        String sql = "insert into %s(select * from %s where 1 = 2)";

        boolean hasInsertPrivilege = true;
        Statement statement = null;
        for (String tableName : tableNameList)
        {
            String checkInsertPrivilegeSql = String.format(sql, tableName, tableName);
            try
            {
                statement = connection.createStatement();
                Jdbc.execute(statement, checkInsertPrivilegeSql);
            } catch (Exception e)
            {
                {
                    hasInsertPrivilege = false;
                    log.warn("User [" + userName + "] has no 'insert' privilege on tableName[" + tableName + "], errorMessage:[{}]", e.getMessage());
                }
            }
        }
        try
        {
            connection.close();
        } catch (SQLException e)
        {
            log.warn("connection close failed, " + e.getMessage());
        }
        return hasInsertPrivilege;
    }

    public static boolean checkDeletePrivilege(String jdbcUrl, String username, String password, List<String> tableNameList)
    {
        Connection connection = Jdbc.getConnection(jdbcUrl, username, password);
        String deleteTemplate = "delete from %s where 1 = 2";

        boolean hasInsertPrivilege = true;
        Statement statement = null;
        for (String tableName : tableNameList)
        {
            String checkDeletePrivilegeSQL = String.format(deleteTemplate, tableName);
            try
            {
                statement = connection.createStatement();
                Jdbc.execute(statement, checkDeletePrivilegeSQL);
            } catch (Exception e)
            {
                hasInsertPrivilege = false;
                log.warn("User [" + username + "] has no 'delete' privilege on tableName[" + tableName + "], errorMessage:[{}]", e.getMessage());
            }
        }
        try
        {
            connection.close();
        } catch (SQLException e)
        {
            log.warn("connection close failed, " + e.getMessage());
        }
        return hasInsertPrivilege;
    }

    public static boolean needCheckDeletePrivilege(Configuration configuration)
    {
        List<String> sqlList = new ArrayList<String>();
        List<String> preSqlList = configuration.getList(Key.pre_sql_list, String.class);
        List<String> postSqlList = configuration.getList(Key.post_sql_list, String.class);
        if (preSqlList != null && !preSqlList.isEmpty())
        {
            sqlList.addAll(preSqlList);
        }
        if (postSqlList != null && !postSqlList.isEmpty())
        {
            sqlList.addAll(postSqlList);
        }
        for (String sql : sqlList)
        {
            if (StringUtils.isNotBlank(sql))
            {
                if (sql.trim().startsWith("delete"))
                {
                    return true;
                }
            }
        }
        return false;
    }
}
